const {ccclass, property} = cc._decorator;
import XmdyPoker from '../XmdyPoker'

@ccclass
export default class Actor extends cc.Component {

    @property(cc.Layout)
    layout:cc.Layout;
    @property(cc.Prefab)
    pokerPrefab:cc.Prefab;


    openSelect:boolean = false;
   
    start(){
        this.node.on(cc.Node.EventType.MOUSE_UP,this.mouseUp,this);
        this.node.on("pokerSelect",this.selectPoker,this);
    }

    selectPokerList:XmdyPoker[] = [];
    selectPoker(event:cc.Event.EventCustom){
        cc.log(" event.target", event.detail)
        let pokerOne = event.detail.getComponent(XmdyPoker);
        this.selectPokerList.push(pokerOne)

    }
    xmdyManager = require('../xmdy/XmdyManager.js');
    
    //选择合理的扑克谈起
    mouseUp(){
        this.openSelect = false;
        let nums:number[] = this.selectPokerList.map(item=>item.sortSize);
        let result:number[] = this.xmdyManager.upPoke(nums)
        //全部不合理就对所有的进行弹起
        if(result.length == 0){
            for(let item of this.selectPokerList){
                item.changeUpOrDown()
            }
        }else{
            //将过滤后的扑克谈起
            let resultPokerList = this.selectPokerList.filter(item=>result.indexOf(item.sortSize)>-1)
            for(let item of resultPokerList){
                item.changeUpOrDown()
            }
        }
      
        this.selectPokerList = []
    }
    onLoad() {
        // init logic
        
    }
    
     //发牌
    public cards(numbers:[{type:number,name:number}]){
        for(let index =0;index<numbers.length;index++){
            let pokerNode:cc.Node = cc.instantiate(this.pokerPrefab);
            let poker = pokerNode.getComponentInChildren(XmdyPoker);
            poker.actor = this;
            poker.setPocker(numbers[index].type,numbers[index].name);
            this.layout.node.addChild(pokerNode);
        }
        
    }
}
